
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.responses import ORJSONResponse
import uvicorn
from servicefix import GitDiff
import os


app = FastAPI()


class Base(BaseModel):
    url: str
    branch: str
    projectName: str
    commitid1: str = None
    commitid2: str = None

""" git创建项目"""
@app.post("/gitProject", response_class=ORJSONResponse)
async def gitProject(base: Base):
    local_path = os.path.join('codes', 'codetest', base.projectName)
    GitDiff(local_path, base.url, base.branch)

'''获取所有分支'''
@app.post("/gitBranchs", response_class=ORJSONResponse)
async def gitBranchs(base: Base):
    local_path = os.path.join('codes', 'codetest', base.projectName)
    result = GitDiff(local_path, base.url, base.branch)
    allBranches = result.branches()
    return allBranches


'''获取所有commitid'''
@app.post("/gitCommits", response_class=ORJSONResponse)
async def gitCommits(base: Base):
    local_path = os.path.join('codes', 'codetest', base.projectName)
    result = GitDiff(local_path, base.url, base.branch)
    allCommits = result.commits()
    return allCommits

'''两次commit对比'''
@app.post("/gitDiffCommitid", response_class=ORJSONResponse)
async def gitDiffCommitid(base: Base):
    local_path = os.path.join('codes', 'codetest', base.projectName)
    result = GitDiff(local_path, base.url, base.branch)
    DiffCommitid = result.commitidDiff(base.commitid1, base.commitid2)
    return DiffCommitid



if __name__ == '__main__':
    uvicorn.run(app='main:app', host="127.0.0.1", port=8000, reload=True, debug=True)
